package xin.yangshuai.swagger2.controller;

import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import xin.yangshuai.swagger2.support.InitData;
import xin.yangshuai.swagger2.support.User;

import java.util.List;

/**
 * HelloController
 *
 * @author shuai
 * @date 2020/5/24
 */
@Api("处理hello相关业务的请求")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Successful — 请求已完成"),
        @ApiResponse(code = 400, message = "请求中有语法问题，或不能满足请求"),
        @ApiResponse(code = 401, message = "未授权客户机访问数据"),
        @ApiResponse(code = 403, message = "资源不可用"),
        @ApiResponse(code = 404, message = "服务器找不到给定的资源；文档不存在"),
        @ApiResponse(code = 500, message = "服务器不能完成请求")}
)
@RestController
@RequestMapping("hello")
public class HelloController {

    @ApiOperation(value = "查询全部用户", notes = "查询全部用户，请求方式：GET，参数：无")
    @GetMapping("users")
    public List<User> list() {
        return InitData.list();
    }

    @ApiOperation(value = "根据用户id查询用户", notes = "查询单一用户，请求方式：GET，参数：id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int", paramType = "path", defaultValue = "0")
    })
    @GetMapping("users/{id}")
    public User get(@PathVariable Integer id) {
        return InitData.get(id);
    }

    @ApiOperation(value = "添加用户", notes = "添加一位用户，请求方式：POST，参数：用户类")
    @PostMapping("users")
    public int insert(@RequestBody User user) {
        return InitData.insert(user);
    }

    @ApiOperation(value = "添加用户", notes = "添加一位用户，请求方式：POST，参数：用户类的id，name，age属性")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "query", defaultValue = "0")
            , @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "string", paramType = "query", defaultValue = "0")
            , @ApiImplicitParam(name = "age", value = "年龄", required = true, dataType = "int", paramType = "query", defaultValue = "0")
    })
    @PostMapping("users/add")
    public int add(User user) {
        return InitData.insert(user);
    }

    @ApiOperation(value = "更新", notes = "更新一位用户，请求方式：PUT，参数：用户类")
    @PutMapping("users")
    public int update(@RequestBody User user) {
        return InitData.update(user);
    }

    @ApiOperation(value = "根据用户id删除用户", notes = "删除单一用户，请求方式：DELETE，参数：id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int", paramType = "path", defaultValue = "0")
    })
    @DeleteMapping("users/{id}")
    public int delete(@PathVariable Integer id) {
        return InitData.delete(id);
    }
}
